package org.extreme.schooltalk.parsing;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.extreme.schooltalk.CommonUtils;
import org.extreme.schooltalk.ParameterManager;
import org.extreme.schooltalk.UserDTO;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.BaseAdapter;

public class SchoolMateParser extends DefaultHandler{
	private String _TAG = null;
	private List<UserDTO> school_list;
	SaxHandler saxHandler;
	Boolean inItem = false;
	UserDTO dto;
	String currentValue = "";
	Handler handler;
	Context mCtx;
	BaseAdapter user_adapter;
	
	public SchoolMateParser(InputStream is){
		try {
			school_list = new ArrayList<UserDTO>();
			saxHandler = new SaxHandler(this,is);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public SchoolMateParser(InputStream is,List<UserDTO> list){
		try{
			school_list = list;
			saxHandler = new SaxHandler(this,is);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public SchoolMateParser(Context ctx,InputStream is,List<UserDTO> list,Handler handler,BaseAdapter user_adapter){
		try{
			school_list = list;
			this.handler = handler;
			this.mCtx = ctx;
			this.user_adapter = user_adapter;
			saxHandler = new SaxHandler(this,is);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public UserDTO getUserDTO(int pos){
		if(school_list == null || school_list.size() == 0)
			return new UserDTO();
		
		return school_list.get(pos);
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if(inItem){			
			currentValue = new String(ch,start,length);
			Log.i(CommonUtils._LOG_TAG,new String(ch,start,length));
		}		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {		
		super.endElement(uri, localName, qName);
		if(localName.equals(SaxHandler._ITEM)){
			school_list.add(dto);						
		}else if(localName.equals(ParameterManager._NICKNAME)){
			dto.setNickname(currentValue);			
		}else if(localName.equalsIgnoreCase(ParameterManager._EMAIL)){
			dto.setEmail(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._PASSWORD)){
			dto.setPassword(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._CONTACT)){
			dto.setContact(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._NICKNAME)){
			dto.setNickname(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._BIRTHDAY)){
			dto.setBirth(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._SEX)){
			dto.setSex(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._INTRODUCE)){
			dto.setIntroduce(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._ELEMENT_SCHOOL)){
			dto.setElement_school(currentValue);		
		}else if(localName.equalsIgnoreCase(ParameterManager._MIDDLE_SCHOOL)){
			dto.setMiddle_school(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._HIGH_SCHOOL)){
			dto.setHigh_school(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._PHOTO_1)){
			dto.setMyPhotoUrl_1(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._PHOTO_2)){
			dto.setMyPhotoUrl_2(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._PHOTO_3)){
			dto.setMyPhotoUrl_3(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._PHOTO_4)){
			dto.setMyPhotoUrl_4(currentValue);
		}else if(localName.equalsIgnoreCase(ParameterManager._REGDATE)){
			dto.setRegdate(currentValue);
		}
		
		inItem  = false;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {		
		super.startElement(uri, localName, qName, attributes);
		inItem = true;
		if(localName.equalsIgnoreCase(SaxHandler._ITEM)){
			dto = new UserDTO();
		}
	}
	
	
	
}
